﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using DingTalkAgent.DBUtility;
namespace DingTalkAgent.SqlData  
{
    //机构与部门管理
    public partial class Department
    {
        #region	 T_Department

        ///<summary>
        /// Exist
        ///</summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Department");
            strSql.Append(" where ");
            strSql.Append(" id = @id  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条T_Department数据
        /// </summary>
        public int Add(DingTalkAgent.Model.Department model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Department(");
            strSql.Append("name,parentid,leveCode,isOrg,order,Remark");
            strSql.Append(") values (");
            strSql.Append("@name,@parentid,@leveCode,@isOrg,@order,@Remark");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@name", SqlDbType.VarChar,64) ,
                        new SqlParameter("@parentid", SqlDbType.Int,4) ,
                        new SqlParameter("@leveCode", SqlDbType.VarChar,32) ,
                        new SqlParameter("@isOrg", SqlDbType.Int,4) ,
                        new SqlParameter("@order", SqlDbType.Int,4) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,1024)

            };

            parameters[0].Value = model.name;
            parameters[1].Value = model.parentid;
            parameters[2].Value = model.leveCode;
            parameters[3].Value = model.isOrg;
            parameters[4].Value = model.order;
            parameters[5].Value = model.Remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条T_Department数据
        /// </summary>
        public bool Update(DingTalkAgent.Model.Department model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Department set ");

            strSql.Append(" name = @name , ");
            strSql.Append(" parentid = @parentid , ");
            strSql.Append(" leveCode = @leveCode , ");
            strSql.Append(" isOrg = @isOrg , ");
            strSql.Append(" order = @order , ");
            strSql.Append(" Remark = @Remark  ");
            strSql.Append(" where id=@id ");

            SqlParameter[] parameters = {
                        new SqlParameter("@id", SqlDbType.Int,4) ,
                        new SqlParameter("@name", SqlDbType.VarChar,64) ,
                        new SqlParameter("@parentid", SqlDbType.Int,4) ,
                        new SqlParameter("@leveCode", SqlDbType.VarChar,32) ,
                        new SqlParameter("@isOrg", SqlDbType.Int,4) ,
                        new SqlParameter("@order", SqlDbType.Int,4) ,
                        new SqlParameter("@Remark", SqlDbType.VarChar,1024)

            };

            parameters[0].Value = model.id;
            parameters[1].Value = model.name;
            parameters[2].Value = model.parentid;
            parameters[3].Value = model.leveCode;
            parameters[4].Value = model.isOrg;
            parameters[5].Value = model.order;
            parameters[6].Value = model.Remark;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条T_Department数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Department ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条T_Department数据
        /// </summary>
        public bool Deletes(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Department ");
            strSql.Append(" where ID in (" + ids + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DingTalkAgent.Model.Department GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, name, parentid, leveCode, isOrg, order, Remark  ");
            strSql.Append("  from T_Department ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;


            DingTalkAgent.Model.Department model = new DingTalkAgent.Model.Department();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                model.name = ds.Tables[0].Rows[0]["name"].ToString();
                if (ds.Tables[0].Rows[0]["parentid"].ToString() != "")
                {
                    model.parentid = int.Parse(ds.Tables[0].Rows[0]["parentid"].ToString());
                }
                model.leveCode = ds.Tables[0].Rows[0]["leveCode"].ToString();
                if (ds.Tables[0].Rows[0]["isOrg"].ToString() != "")
                {
                    model.isOrg = int.Parse(ds.Tables[0].Rows[0]["isOrg"].ToString());
                }
                if (ds.Tables[0].Rows[0]["order"].ToString() != "")
                {
                    model.order = int.Parse(ds.Tables[0].Rows[0]["order"].ToString());
                }
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据T_Department列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Department ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据T_Department列表
        /// </summary>
        public IEnumerable<DingTalkAgent.Model.Department> IEnumGetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, name, parentid, leveCode, isOrg, order, Remark ");
            strSql.Append(" FROM T_Department ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DingTalkAgent.Model.Department model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.Department();
                if (dr["id"].ToString() != "")
                {
                    model.id = int.Parse(dr["id"].ToString());
                }
                model.name = dr["name"].ToString();
                if (dr["parentid"].ToString() != "")
                {
                    model.parentid = int.Parse(dr["parentid"].ToString());
                }
                model.leveCode = dr["leveCode"].ToString();
                if (dr["isOrg"].ToString() != "")
                {
                    model.isOrg = int.Parse(dr["isOrg"].ToString());
                }
                if (dr["order"].ToString() != "")
                {
                    model.order = int.Parse(dr["order"].ToString());
                }
                model.Remark = dr["Remark"].ToString();

                yield return model;
            }
        }
        /// <summary>
        /// 获得数据 T_Department分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="total">总记录数</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public List<DingTalkAgent.Model.Department> GetPageList(int PageIndex, int PageSize, out int total, string keyword)
        {
            int Skip = (PageIndex - 1) * PageSize;
            //count
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select @total=count(id) From T_Department ");
            //data
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " * from (");
            strSql.Append("select  row_number() OVER (ORDER BY id Desc) AS row, id, name, parentid, leveCode, isOrg, order, Remark ");
            strSql.Append(" FROM  T_Department ");
            //where
            StringBuilder Where = new StringBuilder();
            Where.Append(" where 1=1");
            if (keyword.Trim() != "")
            {
                Where.Append(" and (Title like @keyword or content like @keyword) ");
            }


            strSqlCount.Append(Where.ToString());
            strSql.Insert(0, strSqlCount.ToString() + ";");
            strSql.Append(Where.ToString());
            strSql.Append(") as a ");
            strSql.Append(" Where a.row>" + Skip);

            //parameters
            SqlParameter[] parameters = {
                    new SqlParameter("@total", SqlDbType.Int,4),
                    new SqlParameter("@keyword", SqlDbType.VarChar,64)
                    };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = "%" + keyword.Trim() + "%";

            List<DingTalkAgent.Model.Department> list = new List<DingTalkAgent.Model.Department>();
            DingTalkAgent.Model.Department model = new DingTalkAgent.Model.Department();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            total = Convert.ToInt32(parameters[0].Value);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.Department();
                if (dr["id"].ToString() != "")
                {
                    model.id = int.Parse(dr["id"].ToString());
                }
                model.name = dr["name"].ToString();
                if (dr["parentid"].ToString() != "")
                {
                    model.parentid = int.Parse(dr["parentid"].ToString());
                }
                model.leveCode = dr["leveCode"].ToString();
                if (dr["isOrg"].ToString() != "")
                {
                    model.isOrg = int.Parse(dr["isOrg"].ToString());
                }
                if (dr["order"].ToString() != "")
                {
                    model.order = int.Parse(dr["order"].ToString());
                }
                model.Remark = dr["Remark"].ToString();

                list.Add(model);
            }
            return list;
        }
        #endregion

        #region 机构/部门批量添加

        public int AddDepartmentByBatch(List<DingTalkAgent.Model.Department> list)
        {
            int result = 0;
            DataTable dt = new DataTable("udtDepartmentMapping");
            dt.Columns.Add("name");
            dt.Columns.Add("parentid");
            dt.Columns.Add("order");
            foreach (var item in list)
            {
                DataRow row = dt.NewRow();
                row["name"] = item.name;
                row["parentid"] = item.parentid;
                row["order"] = item.order;
                dt.Rows.Add(row);
            }
            IDataParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@p_udtDepartmentMapping", dt);
            DbHelperSQL.RunProcedure("AddDepartmentByBatch", param, out result);
            return result;
        }

        #endregion
    }
}
